<?php


namespace Satan\Think\Addons\controller;


use Satan\Think\Addons\ThinkAddons;
use think\App;
use think\Request;
use think\route\dispatch\Controller;

class AddonsController
{
    /**
     * @var $request Request
     */
    protected Request $request;

    /**
     * @var $app App
     */
    protected App $app;
    public function __construct()
    {
        $this->request = \request();
        $this->app = \app();
    }

    public function execute($addon=null,$controller=null,$action=null)
    {
        $addons = new ThinkAddons($addon);
        /**
         * @var $autoload
         */
        //插件初始化事件
        event('addons_module_init'.$addons);
        //插件request事件
        event('addons_module_request',$this->request);
        //挂载到request上当前的插件管理类
        $this->request->addons = $addons;

        $addons_namespace = $this->check_namespace();
//        dump(\addons\test::class,class_exists(\addons\test::class));
        if ($addon && $controller && $action){
        $this->app->setNamespace($addons_namespace.$addon);
        $controller = new Controller($this->request,$this->request->rule(),[$controller,$action]);
        $controller->init($this->app);
        return $this->app->middleware->pipeline('addons')->send($this->request)
            ->then(function ()use($controller){
                return $controller->run();
            });
        }else{
            abort(500,$this->app->lang->get(('addon can not be empty'),[],'addon'));
        }
    }
    //检测namespace
    private function check_namespace():string
    {
        //检测psr4是否存在addons 没有就手动覆盖掉
        $namespace_file = root_path('vendor/composer').'autoload_psr4.php';
        $namespace = require($namespace_file);
        //插件目录
        $addons_path = config('addons.addons_path')?:'addons';
        //插件命名空间
        $addons_namespace = (config('addons.namespace')?:'addons').'\\';
        return $addons_namespace;
//        dump($addons_namespace,$namespace,array_key_exists($addons_namespace,$namespace),in_array($addons_namespace,$namespace));
        if (!array_key_exists($addons_namespace,$namespace))
        {
            $namespace_body = "
<?php

// autoload_psr4.php @generated by Composer

\$vendorDir = dirname(dirname(__FILE__));
\$baseDir = dirname(\$vendorDir);

return array(
    'think\\view\\driver\\' => array(\$vendorDir . '/topthink/think-view/src'),
    'think\\trace\\' => array(\$vendorDir . '/topthink/think-trace/src'),
    'think\\filesystem\\driver\\' => array(\$vendorDir . '/death_satan/think-qiniu-storage/src'),
    'think\\' => array(\$vendorDir . '/topthink/framework/src/think', \$vendorDir . '/topthink/think-helper/src', \$vendorDir . '/topthink/think-orm/src', \$vendorDir . '/topthink/think-template/src'),
    'app\\' => array(\$baseDir . '/app'),
    '".$addons_namespace."'=>array(\$baseDir.'/".$addons_path."'),
    'Symfony\\Polyfill\\Php80\\' => array(\$vendorDir . '/symfony/polyfill-php80'),
    'Symfony\\Polyfill\\Php72\\' => array(\$vendorDir . '/symfony/polyfill-php72'),
    'Symfony\\Polyfill\\Mbstring\\' => array(\$vendorDir . '/symfony/polyfill-mbstring'),
    'Symfony\\Component\\VarExporter\\' => array(\$vendorDir . '/symfony/var-exporter'),
    'Symfony\\Component\\VarDumper\\' => array(\$vendorDir . '/symfony/var-dumper'),
    'Satan\\Think\\Command\\' => array(\$vendorDir . '/death_satan/thinkphp-extends-command/src'),
    'Satan\\Think\\Addons\\' => array(\$vendorDir . '/death_satan/satan-addons/src'),
    'Psr\\SimpleCache\\' => array(\$vendorDir . '/psr/simple-cache/src'),
    'Psr\\Log\\' => array(\$vendorDir . '/psr/log/Psr/Log'),
    'Psr\\Container\\' => array(\$vendorDir . '/psr/container/src'),
    'Psr\\Cache\\' => array(\$vendorDir . '/psr/cache/src'),
    'League\\MimeTypeDetection\\' => array(\$vendorDir . '/league/mime-type-detection/src'),
    'League\\Flysystem\\Cached\\' => array(\$vendorDir . '/league/flysystem-cached-adapter/src'),
    'League\\Flysystem\\' => array(\$vendorDir . '/league/flysystem/src'),
);


";
            $namespace_body = str_replace('\\','\\\\',$namespace_body);
//            dump($namespace_body);exit;
            //暴力覆盖
            file_put_contents($namespace_file,$namespace_body);
        }
        return $addons_namespace;
    }
}